home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / select / select.c < prev   
Encoding:
C/C++ Source or Header  |  1989-09-06  |  3.0 KB  |  110 lines

  1. /* 
  2.  * select.c --
  3.  *
  4.  *    This file contains a benchmark program to time "select"
  5.  *    calls.  It should be invoked in the following way:
  6.  *
  7.  *    select numPipes numReady count
  8.  *
  9.  *    The program will create numPipes pipes, put data in the
  10.  *    first numReady of them, then do count selects on all of
  11.  *    the pipes with no timeout.  It will print out the average
  12.  *    time for each select operation.
  13.  *
  14.  * Copyright 1989 Regents of the University of California.
  15.  * Permission to use, copy, modify, and distribute this
  16.  * software and its documentation for any purpose and without
  17.  * fee is hereby granted, provided that the above copyright
  18.  * notice appear in all copies.  The University of California
  19.  * makes no representations about the suitability of this
  20.  * software for any purpose.  It is provided "as is" without
  21.  * express or implied warranty.
  22.  */
  23.  
  24. #ifndef lint
  25. static char rcsid[] = "$Header: protoPub.c,v 1.3 87/01/04 17:28:56 andrew Exp $ SPRITE (Berkeley)";
  26. #endif not lint
  27.  
  28. #include <stdio.h>
  29. #include <sys/time.h>
  30. #include <sys/resource.h>
  31.  
  32. main(argc, argv)
  33.     int argc;
  34.     char **argv;
  35. {
  36. #define MAXPIPES 15
  37.     int count, numPipes, numReady, i;
  38.     int readMask, readMask2, result, numFds;
  39.     int pipeStreams[2*MAXPIPES];
  40.     struct timeval start, stop;
  41.     struct rusage begin ,end;
  42.     static struct timeval timeout = {0,0};
  43.     struct timezone tz;
  44.     int micros;
  45.     double timePer;
  46.  
  47.     if (argc != 4) {
  48.     fprintf(stderr, "Usage: select numPipes numReady count\n");
  49.     exit(1);
  50.     }
  51.  
  52.     numPipes = atoi(argv[1]);
  53.     if (numPipes > MAXPIPES) {
  54.     fprintf(stderr, "Don't ask for more than %d pipes\n", MAXPIPES);
  55.     exit(1);
  56.     }
  57.     numReady = atoi(argv[2]);
  58.     if ((numReady < 0) || (numReady > numPipes)) {
  59.     fprintf(stderr,
  60.         "Number of ready pipes must be between 0 and %d\n",
  61.         numPipes);
  62.     exit(1);
  63.     }
  64.     count = atoi(argv[3]);
  65.  
  66.     for (i = 0, readMask = 0, numFds = 0; i < numPipes; i++) {
  67.     if (pipe(&pipeStreams[2*i]) == -1) {
  68.         fprintf(stderr, "Couldn't create pipes\n");
  69.         exit(1);
  70.     }
  71.     readMask |= 1 << pipeStreams[2*i];
  72.     if (pipeStreams[2*i] >= numFds) {
  73.         numFds = pipeStreams[2*i] + 1;
  74.     }
  75.     if (i < numReady) {
  76.         char c;
  77.  
  78.         c = 0;
  79.         if (write(pipeStreams[2*i + 1], &c, 1) != 1) {
  80.         fprintf(stderr, "Couldn't write to pipe\n");
  81.         exit(1);
  82.         }
  83.     }
  84.     }
  85.  
  86. #ifdef GETRUSAGE
  87.     getrusage(RUSAGE_SELF, &begin);
  88. #else
  89.     gettimeofday(&start, (struct timezone *) NULL);
  90. #endif
  91.     for (i = 0; i < count; i++) {
  92.     readMask2 = readMask;
  93.     result = select(numFds, &readMask2, 0, 0, &timeout);
  94.     }
  95. #ifdef GETRUSAGE
  96.     getrusage(RUSAGE_SELF, &end);
  97.     micros = (end.ru_utime.tv_sec + end.ru_stime.tv_sec
  98.         - begin.ru_utime.tv_sec - begin.ru_stime.tv_sec)*1000000
  99.         + (end.ru_utime.tv_usec - begin.ru_utime.tv_usec)
  100.         + (end.ru_stime.tv_usec - begin.ru_stime.tv_usec);
  101. #else
  102.     gettimeofday(&stop, (struct timezone *) NULL);
  103.     micros = 1000000*(stop.tv_sec - start.tv_sec)
  104.         + stop.tv_usec - start.tv_usec;
  105. #endif
  106.     timePer = micros;
  107.     printf("Time per iteration: %.2f microseconds\n", timePer/count);
  108.     printf("Last result was %d\n", result);
  109. }
  110.